package fr.nantes.univ.DAO;

import java.util.List;

import javax.persistence.EntityManager;

import fr.nantes.univ.EMF;
import fr.nantes.univ.EntityBeans.Address;

public class AddressDAO {
	
	private static EntityManager instance = EMF.get().createEntityManager();
	
	public static List<Address> findByUser(String userId) {
		List<Address> addresses = instance
				.createQuery("select a from Address a where a.userId = :userId", Address.class)
				.setParameter("userId", userId)
				.getResultList();
		return addresses;
	}
	
	public static Address findByAddress(String _address) {
		Address address = instance
				.createQuery("select a from Address a where a.address = :address", Address.class)
				.setParameter("address", _address)
				.getSingleResult();
		return address;
	}
	
	public static void delete(Address address) {
		instance.getTransaction().begin();
		instance.remove(address);
		instance.getTransaction().commit();
	}
	
	public static void insert(Address address) {
		instance.getTransaction().begin();
		instance.persist(address);
		instance.getTransaction().commit();
	}

}
